package com.prs.crm.action.sale;

import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.annotation.Clew;
import com.prs.crm.domain.sale.SaleInvoices;
import com.prs.crm.domain.sale.SalesReceivesRegistBillReality;
import com.prs.crm.domain.sys.Code;
import com.prs.crm.domain.sys.CodeInvoice;
import com.prs.crm.service.sale.SaleInvoicesService;
import com.prs.crm.service.sale.SalesReceivesRegistBillRealityService;
import com.prs.crm.service.sys.CodeInvoiceService;
import com.prs.crm.service.sys.CodeService;

public class SaleInvoicesAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	
	private String Encode;
	
	private CodeInvoice codeInvoice;
	
	private CodeInvoiceService codeInvoiceService;
	
	private Code code;
	
	private CodeService codeService;
	
	private Collection<CodeInvoice> codelist;
	
	private SaleInvoicesService saleInvoicesService;

	private SalesReceivesRegistBillRealityService salesReceivesRegistBillRealityService;

	private SalesReceivesRegistBillReality salesReceivesRegistBillReality;

	private SaleInvoices saleInvoices;
	private int type;

	private PaginationSupport pagination;

	@Clew(results = { "success", "none" })
	@Action(value = "saveSalesInvoices", results = {
			@Result(name = "success", type = "redirect", location = "listSalesInvoices"),
			@Result(name="error",type="chain",location="SaleInvoices"),
			@Result(name = "none", type = "redirect", location = "listSalesInvoices") })
	public String saveSalesInvoices() {
		if ("n".equals(Encode)) {
			int EncodeRplice = this.getCodeService().getBackRpliceAll("手动编码",
					"销售开票",saleInvoices.getInvoiceCode());
			if (EncodeRplice != 0) {
				this.addActionError("编码重复!请重新输入");
				return ERROR;
			}
		}
		
		if("x".equals(Encode)||"".equals(Encode)){
			this.addActionError("请选择编码类型");		
			return ERROR;
		}
		if(saleInvoices.getCheckDate() == null) {
			this.addActionError("");
			return ERROR;
		}
		if(saleInvoices.getInvoiceCode() == null || saleInvoices.getInvoiceCode().equals("")) {
			this.addActionError("");
			return ERROR;
		}
		SalesReceivesRegistBillReality objext = this
				.getSalesReceivesRegistBillRealityService()
				.getSalesReceivesRegistBillReality(
						salesReceivesRegistBillReality.getId());
		objext.setJudgeValue(1);
		

		/**
		 * 编码产生
		 */
		if ("n".equals(Encode)) {
			Code codes = new Code();
			codes.setCodeValue(saleInvoices.getInvoiceCode());
		    codes.setCodeMolde("销售开票");
		    codes.setEntityName(this.getClass().getName());
			codes.setCodeType("手动编码");
			this.getCodeService().save(codes);
			saleInvoices.setCode(codes);
			this.getSaleInvoicesService().save(saleInvoices);
		} else {
			CodeInvoice Vob = this.getCodeInvoiceService().getVobject(
					codeInvoice.getId());
			Code codes = new Code();
			codes.setCodeValue(saleInvoices.getInvoiceCode());
			codes.setCodeMolde("销售开票");
			codes.setEntityName(this.getClass().getName());
			Vob.setNumBackUp(Increase(Vob.getNumBackUp()));
			this.getCodeInvoiceService().saveOrUpdate(Vob);
			codes.setCodeType(Vob.getCodePlan());
			this.getCodeService().save(codes);
			saleInvoices.setCode(codes);
			this.getSaleInvoicesService().save(saleInvoices);
		}
		this.setPrompt("操作成功");
		return SUCCESS;
	}

	@Action("viewSalesInvoices")
	public String viewSalesInvoices() {
		saleInvoices = this.getSaleInvoicesService().get(saleInvoices.getId());

		return SUCCESS;
	}

	@Action("listSalesInvoices")
	public String excute3() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(
				SaleInvoices.class);
		pagination = this.getPaginationHelper().prepare(pagination);

		this.setPagination(this.getSaleInvoicesService().listByPage(criteria,
				pagination));

		return SUCCESS;
	}
	public static String Increase(String backupNum) {
		String Inc = "";
		int lengths = backupNum.length();
		String lengthss = (Integer.parseInt(backupNum) + 1) + "";
		int increase = (lengthss.trim()).length();
		if (lengths != increase) {
			String In = "";
			for (int i = 0; i < lengths - increase; i++) {
				In = In + "0";
			}
			In = (In + lengthss).trim();
			Inc = In;
		} else {
			Inc = lengthss;
		}
		return Inc.trim();
	}
	public SaleInvoicesService getSaleInvoicesService() {
		return saleInvoicesService;
	}

	public void setSaleInvoicesService(SaleInvoicesService saleInvoicesService) {
		this.saleInvoicesService = saleInvoicesService;
	}

	public SaleInvoices getSaleInvoices() {
		return saleInvoices;
	}

	public void setSaleInvoices(SaleInvoices saleInvoices) {
		this.saleInvoices = saleInvoices;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public SalesReceivesRegistBillRealityService getSalesReceivesRegistBillRealityService() {
		return salesReceivesRegistBillRealityService;
	}

	public void setSalesReceivesRegistBillRealityService(
			SalesReceivesRegistBillRealityService salesReceivesRegistBillRealityService) {
		this.salesReceivesRegistBillRealityService = salesReceivesRegistBillRealityService;
	}

	public SalesReceivesRegistBillReality getSalesReceivesRegistBillReality() {
		return salesReceivesRegistBillReality;
	}

	public void setSalesReceivesRegistBillReality(
			SalesReceivesRegistBillReality salesReceivesRegistBillReality) {
		this.salesReceivesRegistBillReality = salesReceivesRegistBillReality;
	}

	public int getType() {
		return type;
	}

	public void setType(int type) {
		this.type = type;
	}

	public String getEncode() {
		return Encode;
	}

	public void setEncode(String encode) {
		Encode = encode;
	}

	public CodeInvoice getCodeInvoice() {
		return codeInvoice;
	}

	public void setCodeInvoice(CodeInvoice codeInvoice) {
		this.codeInvoice = codeInvoice;
	}

	public CodeInvoiceService getCodeInvoiceService() {
		return codeInvoiceService;
	}

	public void setCodeInvoiceService(CodeInvoiceService codeInvoiceService) {
		this.codeInvoiceService = codeInvoiceService;
	}

	public Code getCode() {
		return code;
	}

	public void setCode(Code code) {
		this.code = code;
	}

	public CodeService getCodeService() {
		return codeService;
	}

	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	public Collection<CodeInvoice> getCodelist() {
		return codelist;
	}

	public void setCodelist(Collection<CodeInvoice> codelist) {
		this.codelist = codelist;
	}

}
